﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LinkedCarriageCration : MonoBehaviour {

	//this list will hold all all the train carriage links that shane currently holds
	List<GameObject> attachedLinks = new List<GameObject> ();
	//this is a prefab of the basic linker that will be created for each new carriage
	public GameObject linker;
	public GameObject Carriage;

	void Update () 
	{
		if(Input.GetKeyDown (KeyCode.T))
		{
			CreateCarriage( Carriage);
		}

		if(Input.GetKeyDown (KeyCode.Y))
		{
			DestroyCarriages();
		}
	}

	//this function will ccreate a linker that wil make its own carriage, this function will tell the linker what it is supposed to follow
	public void CreateCarriage(GameObject carriageType, Transform starTrans = null)
	{
		//create a reference to what the linker will attach to then set it to the last linked carriage or to this
		Transform linkTo;
		if(attachedLinks.Count > 0)
		{
			int lastIndex = attachedLinks.Count - 1;
			linkTo = attachedLinks[lastIndex].GetComponent<TrainLinkerScript>().imPulling;
		}
		else
		{
			linkTo = this.transform;
		}

		//create a new linker and attach it to its designated object to attach to
		Vector3 createPosition = linkTo.position - linkTo.forward;
		GameObject instance = Instantiate (linker, createPosition, linkTo.transform.rotation) as GameObject;
		instance.GetComponent<TrainLinkerScript> ().WasCreated (linkTo, carriageType, attachedLinks.Count, this, starTrans); 
		attachedLinks.Add (instance);

	}

	//this function will ccreate a linker that wil make its own carriage, this function will tell the linker what it is supposed to follow
	public void CreateCarriage(Transform starTrans = null)
	{
		//create a reference to what the linker will attach to then set it to the last linked carriage or to this
		Transform linkTo;
		if(attachedLinks.Count > 0)
		{
			int lastIndex = attachedLinks.Count - 1;
			linkTo = attachedLinks[lastIndex].GetComponent<TrainLinkerScript>().imPulling;
		}
		else
		{
			linkTo = this.transform;
		}
		
		//create a new linker and attach it to its designated object to attach to
		Vector3 createPosition = linkTo.position - linkTo.forward;
		GameObject instance = Instantiate (linker, createPosition, linkTo.transform.rotation) as GameObject;
		instance.GetComponent<TrainLinkerScript> ().WasCreated (linkTo, Carriage, attachedLinks.Count, this, starTrans); 
		attachedLinks.Add (instance);
		
	}

	public void DestroyCarriages()
	{
		for(int i = attachedLinks.Count - 1; i >-1 ; i--)
		{
			GameObject link = attachedLinks[i];
			//attachedLinks.RemoveAt(i);
			link.GetComponent<TrainLinkerScript>().DestroyLinkAndCarriage();
		}

	}

	public void RemoveThisLink(GameObject link)
	{
		int i = link.GetComponent<TrainLinkerScript> ().myIndex;
		attachedLinks.RemoveAt(i);
		if(i < attachedLinks.Count)
		{
			if(i>0)
			{
				attachedLinks[i].GetComponent<TrainLinkerScript>().AttachToObject(attachedLinks[i-1].GetComponent<TrainLinkerScript>().imPulling);
			}
			else
			{
				attachedLinks[i].GetComponent<TrainLinkerScript>().AttachToObject(transform);
			}

			for(int j=i; j< attachedLinks.Count; j++)
			{
				attachedLinks [j].GetComponent<TrainLinkerScript> ().myIndex = j;
			}
		}

	}

}
